package j2025.j05;

import javax.swing.tree.TreeNode;

/**
 * @Author 猫大仙
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
public class j0510 {
    private int max = Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        dfs(root);
        return max;
    }
    private int dfs(TreeNode root){
        if(root==null){
            return 0;
        }
        int left = Math.max(0,dfs(root.left));
        int right = Math.max(0,dfs(root.right));
        int curboth = root.val+left+right;
        int curone = root.val+Math.max(left,right);
        max = Math.max(max,curboth);
        return curone>=0?curone:0;
    }
}
